package asset

import (
	"fmt"
	"strconv"
	. "test_client/logic/internal"
)

type assetCommand struct {
}

func init() {
	Skeleton.AfterInit(func() {
		command := new(assetCommand)
		Skeleton.RegisterCommand("asset.add", "input add asset(id, count)", command.add)
		Skeleton.RegisterCommand("asset.remove", "input remove asset(id, count, entity)", command.remove)
	})
}

func (*assetCommand) add(args []interface{}) (result interface{}) {
	const argsCount = 2
	if len(args) != argsCount {
		result = fmt.Sprintf("arg count less %v", argsCount)
		return
	}

	first := args[0].(string)
	id, err := strconv.Atoi(first)
	if err != nil {
		return err.Error()
	}

	second := args[1].(string)
	count, err := strconv.Atoi(second)
	if err != nil {
		return err.Error()
	}

	rpc.TestAdd(id, count)

	return
}

func (*assetCommand) remove(args []interface{}) (result interface{}) {
	const argsCount = 2
	argLength := len(args)
	if argLength < argsCount {
		result = fmt.Sprintf("arg count less %v", argsCount)
		return
	}

	first := args[0].(string)
	id, err := strconv.Atoi(first)
	if err != nil {
		return err.Error()
	}

	second := args[1].(string)
	count, err := strconv.Atoi(second)
	if err != nil {
		return err.Error()
	}

	var entity string
	if argLength > argsCount {
		entity = args[2].(string)
	}

	rpc.TestRemove(id, count, entity)

	return
}
